import java.io.*;

/**
 * 存储文件,也就是像一个文件中写入内容,既然是写,肯定要用输出流.
 * 而我们写的是纯文本文件,所以这里使用字符流操作.FileWriter
 *
 * Created by ruwenbo on 2018/6/18.
 */
public class TestFileWriter {

    public static void main(String[] args) throws IOException {

        writeToFile();

        readFromFile();
    }

    /**
     * 从文件中读取内容
     */
    private static void readFromFile() throws IOException {

        File file = new File("E:\\", "demo.txt");
        FileReader reader = new FileReader(file);
        char[] container = new char[1024];
        int read = 0;
        StringBuilder sb = new StringBuilder();
        while ((read = reader.read(container)) != -1) {
            sb.append(container, 0, read);
        }
        System.out.println(sb.toString());
    }

    /**
     * 往文件中写入内容
     */
    private static void writeToFile() throws IOException {

        String writeContent = "Hello, 你好世界";
        File file = new File("E:\\demo.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file);
        writer.write(writeContent);
        writer.flush();
        writer.close();
    }
}
